import java.util.*;
import java.io.*;

public class Movie {
    private String id;
    private String name;
    private String director;
    private String genre;
    private int duration; // 时长（分钟）
    private String description;
    
    // 静态列表存储所有电影
    private static List<Movie> movies = new ArrayList<>();
    private static final String MOVIE_FILE = "data/movies.txt";
    
    static {
        loadMovies();
        // 如果没有数据，添加一些默认电影
        if (movies.isEmpty()) {
            movies.add(new Movie("1", "泰坦尼克号", "詹姆斯·卡梅隆", "爱情/灾难", 194, "经典爱情灾难片"));
            movies.add(new Movie("2", "阿凡达", "詹姆斯·卡梅隆", "科幻/冒险", 162, "3D科幻巨制"));
            movies.add(new Movie("3", "复仇者联盟", "乔斯·韦登", "动作/科幻", 143, "漫威超级英雄电影"));
            saveMovies();
        }
    }
    
    public Movie(String id, String name, String director, String genre, int duration, String description) {
        this.id = id;
        this.name = name;
        this.director = director;
        this.genre = genre;
        this.duration = duration;
        this.description = description;
    }
    
    // Getters
    public String getId() { return id; }
    public String getName() { return name; }
    public String getDirector() { return director; }
    public String getGenre() { return genre; }
    public int getDuration() { return duration; }
    public String getDescription() { return description; }
    
    // Setters
    public void setName(String name) { this.name = name; }
    public void setDirector(String director) { this.director = director; }
    public void setGenre(String genre) { this.genre = genre; }
    public void setDuration(int duration) { this.duration = duration; }
    public void setDescription(String description) { this.description = description; }
    
    // 静态方法：获取所有电影
    public static List<Movie> getMovies() {
        return movies;
    }
    
    // 静态方法：添加电影
    public static void addMovie(Scanner scanner) {
        System.out.println("请输入电影ID：");
        String id = scanner.nextLine();
        
        // 检查ID是否已存在
        for (Movie m : movies) {
            if (m.getId().equals(id)) {
                System.out.println("电影ID已存在，请使用其他ID。");
                return;
            }
        }
        
        System.out.println("请输入电影名称：");
        String name = scanner.nextLine();
        System.out.println("请输入导演：");
        String director = scanner.nextLine();
        System.out.println("请输入类型：");
        String genre = scanner.nextLine();
        System.out.println("请输入时长（分钟）：");
        int duration;
        try {
            duration = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("时长输入有误。");
            return;
        }
        System.out.println("请输入描述：");
        String description = scanner.nextLine();
        
        Movie movie = new Movie(id, name, director, genre, duration, description);
        movies.add(movie);
        saveMovies();
        System.out.println("电影添加成功。");
    }
    
    // 静态方法：删除电影
    public static void deleteMovie(Scanner scanner) {
        System.out.println("请输入电影ID：");
        String id = scanner.nextLine();
        
        Movie toDelete = null;
        for (Movie m : movies) {
            if (m.getId().equals(id)) {
                toDelete = m;
                break;
            }
        }
        
        if (toDelete == null) {
            System.out.println("未找到该电影。");
            return;
        }
        
        System.out.printf("确定要删除电影 %s 吗？(y/n): ", toDelete.getName());
        String confirm = scanner.nextLine();
        if (confirm.equalsIgnoreCase("y")) {
            movies.remove(toDelete);
            saveMovies();
            System.out.println("电影已删除。");
        } else {
            System.out.println("操作已取消。");
        }
    }
    
    // 静态方法：修改电影
    public static void modifyMovie(Scanner scanner) {
        System.out.println("请输入电影ID：");
        String id = scanner.nextLine();
        
        Movie toModify = null;
        for (Movie m : movies) {
            if (m.getId().equals(id)) {
                toModify = m;
                break;
            }
        }
        
        if (toModify == null) {
            System.out.println("未找到该电影。");
            return;
        }
        
        System.out.printf("当前名称: %s, 请输入新名称（回车跳过）：\n", toModify.getName());
        String name = scanner.nextLine();
        if (!name.isEmpty()) {
            toModify.setName(name);
        }
        
        System.out.printf("当前导演: %s, 请输入新导演（回车跳过）：\n", toModify.getDirector());
        String director = scanner.nextLine();
        if (!director.isEmpty()) {
            toModify.setDirector(director);
        }
        
        System.out.printf("当前类型: %s, 请输入新类型（回车跳过）：\n", toModify.getGenre());
        String genre = scanner.nextLine();
        if (!genre.isEmpty()) {
            toModify.setGenre(genre);
        }
        
        System.out.printf("当前时长: %d, 请输入新时长（回车跳过）：\n", toModify.getDuration());
        String durationStr = scanner.nextLine();
        if (!durationStr.isEmpty()) {
            try {
                int duration = Integer.parseInt(durationStr);
                toModify.setDuration(duration);
            } catch (NumberFormatException e) {
                System.out.println("时长输入有误，未修改。");
            }
        }
        
        System.out.printf("当前描述: %s, 请输入新描述（回车跳过）：\n", toModify.getDescription());
        String description = scanner.nextLine();
        if (!description.isEmpty()) {
            toModify.setDescription(description);
        }
        
        saveMovies();
        System.out.println("电影信息已修改。");
    }
    
    // 静态方法：列出所有电影
    public static void listAllMovies() {
        if (movies.isEmpty()) {
            System.out.println("当前没有电影信息。");
            return;
        }
        System.out.println("\n所有电影信息:");
        for (Movie m : movies) {
            System.out.printf("ID: %s, 名称: %s, 导演: %s, 类型: %s, 时长: %d分钟\n",
                m.getId(), m.getName(), m.getDirector(), m.getGenre(), m.getDuration());
        }
    }
    
    // 静态方法：查询电影
    public static void queryMovie(Scanner scanner) {
        System.out.println("请输入电影名称（支持模糊查询）：");
        String keyword = scanner.nextLine().toLowerCase();
        
        List<Movie> results = new ArrayList<>();
        for (Movie m : movies) {
            if (m.getName().toLowerCase().contains(keyword) ||
                m.getDirector().toLowerCase().contains(keyword) ||
                m.getGenre().toLowerCase().contains(keyword)) {
                results.add(m);
            }
        }
        
        if (results.isEmpty()) {
            System.out.println("未找到相关电影。");
            return;
        }
        
        System.out.println("\n查询结果:");
        for (Movie m : results) {
            System.out.printf("ID: %s, 名称: %s, 导演: %s, 类型: %s, 时长: %d分钟\n",
                m.getId(), m.getName(), m.getDirector(), m.getGenre(), m.getDuration());
        }
    }
    
    // 文件持久化方法
    private static void saveMovies() {
        try {
            File dataDir = new File("data");
            if (!dataDir.exists()) {
                dataDir.mkdirs();
            }
            
            FileWriter writer = new FileWriter(MOVIE_FILE);
            for (Movie movie : movies) {
                writer.write(String.format("%s|%s|%s|%s|%d|%s\n",
                    movie.getId(), movie.getName(), movie.getDirector(),
                    movie.getGenre(), movie.getDuration(), movie.getDescription()));
            }
            writer.close();
        } catch (IOException e) {
            System.out.println("保存电影数据失败: " + e.getMessage());
        }
    }
    
    private static void loadMovies() {
        try {
            File file = new File(MOVIE_FILE);
            if (!file.exists()) {
                return;
            }
            
            Scanner fileScanner = new Scanner(file);
            while (fileScanner.hasNextLine()) {
                String line = fileScanner.nextLine();
                String[] parts = line.split("\\|");
                if (parts.length >= 6) {
                    String id = parts[0];
                    String name = parts[1];
                    String director = parts[2];
                    String genre = parts[3];
                    int duration = Integer.parseInt(parts[4]);
                    String description = parts[5];
                    movies.add(new Movie(id, name, director, genre, duration, description));
                }
            }
            fileScanner.close();
        } catch (IOException e) {
            System.out.println("加载电影数据失败: " + e.getMessage());
        }
    }
}
